<?php

class PageModel extends MY_Model
{
	public $data;	
			
	/**
	 * @name initialize
	 * @description To initialze the Calendar object.
	 *
	 */
	public function initialize()
	{

	}
	
	public function authLogin($username, $password)
	{
		$this->db->select('password');
		$this->db->from('admin_users');
		$this->db->where('user_email', $username);
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			$correct_password = $result[0]['password'];
			if(trim($password) != '' && $correct_password == $password)
			{
				return true;
			}
		}
		return false;
		
	}
	
	function showTestTable()
	{
		$this->db->select('value');
		$this->db->from('test');
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return $query->result_array();			
		}
	}

	function insertNewPage($data)
	{
		$this->db->set('controller', $data['controller']);
		$this->db->set('action',$data['action']);
		$this->db->insert('pages');
	}
	
	function insertNewState($data)
	{
		$this->db->set('name', $data['name']);
		$this->db->set('value',$data['value']);
		$this->db->insert('states');
	}
	
	function insertNewMessage($data)
	{
		$this->db->set('name', $data['name']);
		if(trim($data['type']) != '')
			$this->db->set('type',$data['type']);
		$this->db->set('value',$data['value']);
		$this->db->insert('messages');
	}
	
	
	function insertPageMessage($data)
	{
		$this->db->set('page_id', $data['page_id']);
		$this->db->set('message_id',$data['message_id']);
		$this->db->set('variable_name',$data['variable_name']);
		$this->db->insert('page_message');
	}
	
	function deletePageMessage($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('page_message');
	}
	
	function getPageMessages()
	{
		$this->db->select('page_message.id, page_message.variable_name, pages.action, pages.controller, messages.name,messages.value');
		$this->db->from('page_message');
		$this->db->join('pages', 'page_message.page_id = pages.id');
		$this->db->join('messages', 'page_message.message_id = messages.id');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function getPages()
	{
		$query = $this->db->get('pages');
		return $query->result_array();
	}
	
	function getMessages()
	{
		$query = $this->db->get('messages');
		return $query->result_array();
	}
	
	function modifyMessage($data)
	{
		$this->db->where('id', $data['id']);
		if(trim($data['type']) != '')
			$this->db->set('type', $data['type']);
		$this->db->set('value', $data['value']);
		$this->db->update('messages');
	}
	
	
	
	/**
	 * @name - apiCalendar
	 * @description - To get the list of scheduled matches from db and return it to respective controllers.
	 *
	 */
/*	public function apiCalendar()
	{			
		//load various required model to use its methods for fetching the required data from API
		require(APPPATH."models/clubsmodel.php");
		$clubs = new ClubsModel();
		$clubs->initialize();
		$this->db->select("matches.id, matches.club1_id, matches.club2_id, matches.start_datetime, venues.description as venue, match_types.brief as match_type");
		$this->db->from("matches");
		$this->db->join("match_types", "match_types.id = matches.match_type_id");
		$this->db->join("tournament","tournament.id = matches.tournament_id");
		$this->db->join("venues","venues.id = matches.venue_id");
		$this->db->order_by("matches.start_datetime","ASC");

		$query = $this->db->get();
		foreach($query->result() as $row)
		{
			$row->club1_desc = $clubs->data[$row->club1_id]->description;
			$row->club2_desc = $clubs->data[$row->club2_id]->description;
			$row->start_datetime = date("d M, h:i A",strtotime($row->start_datetime));
			$data[] = $row;
		}
		return $data;
	}*/

	

	/**
	 * @name - apiLiveMatches
	 * @description - To get the list of Live Matches currently going on.
	 *
	 */
	/*public function apiLiveMatches()
	{			
		// load various required model to use its methods for fetching the required data from API
		// Ex. West Indies vs South Africa / SA: 198/2 Ovs: 73.2 
		// create a query to fetch data from DB
		require(APPPATH."models/clubsmodel.php");
		$clubs = new ClubsModel();
		$clubs->initialize();
		//select matches.club1_id, matches.club2_id, innings.runs_made, innings.overs_played, innings.wickets from innings, matches where matches.is_live = 1 and matches.id = innings.match_id
		$this->db->select("matches.id, matches.club1_id, matches.club2_id, innings.runs_made, innings.overs_played, innings.wickets");
		$this->db->from("matches");
		$this->db->join("innings", "innings.match_id = matches.id");
		$this->db->where("matches.is_live = 1");
		$query = $this->db->get();
		foreach($query->result() as $row)
		{
			$row->club1_desc = $clubs->data[$row->club1_id]->description;
			$row->club2_desc = $clubs->data[$row->club2_id]->description;
			$row->club1 = $clubs->data[$row->club1_id]->brief;
			$row->club2 = $clubs->data[$row->club2_id]->brief;			
			$data[] = $row;
		}
		return $data;		
	}*/
	
	
	/*public function apiMatchDetail($match_id)
	{			
		$query = $this->db->query("call getLiveScore($match_id)");
		$data = $query->result_object();
		return $data;		
	}
	
	public function apiCurrentPlayers($striker_id,$non_striker_id,$current_bowler_id)
	{
		$query = $this->db->query("call getCurrentPlayers($striker_id,$non_striker_id,$current_bowler_id)");
		$data = $query->result_object();
		return $data;
	}*/
	

}
?>
